package com.yunkaweilai.android.fragment.member;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.member.MemberRechargeActivity;
import com.yunkaweilai.android.base.BaseFragment;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.model.member.MemberHeadModel;
import com.yunkaweilai.android.model.member.PremiumRechargeModel;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;
import com.zhy.adapter.abslistview.CommonAdapter;
import com.zhy.adapter.abslistview.ViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 优惠充值
 * Created by Administrator on 2017/6/28.
 */

public class PremiumRechargeFragment extends BaseFragment {
    @BindView(R.id.id_listview)
    ListView idListview;
    @BindView(R.id.id_img_nothing)
    ImageView imgnothing;
    @BindView(R.id.id_line_nothing)
    LinearLayout linearLayoutNothing;
    @BindView(R.id.id_tv_nothing)
    TextView tvnothing;
    Unbinder unbinder;
    private ArrayList<PremiumRechargeModel.DataBean.ListBean> list = new ArrayList<>();
    private CommonAdapter<PremiumRechargeModel.DataBean.ListBean> adapter;
    private MemberHeadModel model;

    public static PremiumRechargeFragment getInstence(MemberHeadModel models){
        PremiumRechargeFragment fragment = new PremiumRechargeFragment();
        fragment.model = models;
        return fragment;
    }

    @Override
    public int getContentViewId() {
        return R.layout.fragment_preferential_activities;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        imgnothing.setImageResource(R.mipmap.ic_no_recharge);
        tvnothing.setText(Html.fromHtml("无优惠充值<br>在“会员等级>储值功能”中设置"));
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initadapter();
        initHttp();
    }

    private void initHttp() {
        HttpRequest.postUrl(Api.PREMIUM_RECHARGE)
                .addParams("level_id",model.getLevel_id())
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(mAc, json);
                        if (checkCode){
                            Gson gson = new Gson();
                            PremiumRechargeModel premiumRechargeModel = gson.fromJson(json, PremiumRechargeModel.class);
                            list.clear();
                            list.addAll(premiumRechargeModel.getData().getList());
                            if (list.size()>0){
                                idListview.setVisibility(View.VISIBLE);
                                linearLayoutNothing.setVisibility(View.GONE);
                            }else {
                                idListview.setVisibility(View.GONE);
                                linearLayoutNothing.setVisibility(View.VISIBLE);
                            }
                            adapter.notifyDataSetChanged();
                        }
                    }

                    @Override
                    public void onError(Exception e) {

                    }
                });
    }

    private void initadapter() {
        adapter = new CommonAdapter<PremiumRechargeModel.DataBean.ListBean>(mAc,R.layout.item_list_preferential_activities,list) {
            @Override
            protected void convert(ViewHolder viewHolder, PremiumRechargeModel.DataBean.ListBean item, int position) {
                viewHolder.setText(R.id.id_tv_activity_name,item.getRule_content());
                viewHolder.setText(R.id.id_tv_activity_money,item.getPrice()+"");
                viewHolder.setText(R.id.id_tv_activity_give_money,item.getSending_price()+"");
                viewHolder.setText(R.id.id_tv_activity_all_money,item.getTotle_price());
            }
        };
        idListview.setAdapter(adapter);

        idListview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PremiumRechargeModel.DataBean.ListBean listBean = list.get(position);
                Intent intent = new Intent();
                intent.putExtra(MemberRechargeActivity.PAY_TYPE_ID, listBean.getId());
                intent.putExtra(MemberRechargeActivity.PAY_TYPE_MONEY, listBean.getPrice());
                intent.putExtra(MemberRechargeActivity.PAY_TYPE_GIVE_MONEY,listBean.getSending_price());
                intent.putExtra(MemberRechargeActivity.PAY_TYPE_NAME,listBean.getRule_content());
                intent.putExtra(MemberRechargeActivity.PAY_TYPE_RULE,listBean.getRecharge_type()+"");
                intent.putExtra(MemberRechargeActivity.PAY_TYPE_ALL_MONEY,listBean.getTotle_price() + "");
                intent.putExtra(MemberRechargeActivity.PAY_TYPE_IS_TRUE,true);
                getActivity().setResult(Activity.RESULT_OK, intent);
                getActivity().finish();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
